package com.szqhyxj.youxiangju.biz.merchantHome;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.BasePresenterImpl;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;
import com.szqhyxj.youxiangju.biz.login.LoginModel;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceModel;
import com.szqhyxj.youxiangju.biz.merchantHome.notice.NoticeModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.umeng.analytics.MobclickAgent;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/10/30.
 */

public class MerchantContainerPresenter extends BasePresenterImpl implements MerchantContainerContract.Presenter {

    private MerchantContainerContract.View view;

    MerchantContainerPresenter(MerchantContainerContract.View view) {
        this.view = view;
    }

    @Override
    public void setHome() {
        this.view.showHome();
    }

    @Override
    public void setShop() {
        if (preCheck()) {
            return;
        }
        if (ispartner()){
            return;
        }
        view.showShop();
    }

    @Override
    public void setPlan() {
        if (preCheck()) {
            return;
        }
        view.showPlan();
    }

    @Override
    public void setMy() {

        this.view.showMy();
    }

    private boolean preCheck() {
        if (!TokenManager.INSTANCE.online()) {
            view.navToLogin();
            return true;
        }
        if (!UserInfoManger.INSTANCE.isRealName()) {
            view.guideToRealName();
            return true;
        }
        return false;
    }
    private boolean ispartner() {
        getUserInfo();
        int type = UserInfoManger.INSTANCE.getUser().getUserType();

        if (UserInfoManger.INSTANCE.getUser().getPartnerActivity()==0){
            view.showShop();
            return true;
        }else{
            if (UserInfoManger.INSTANCE.getUser().getUserType()==4||UserInfoManger.INSTANCE.getUser().getUserType()==5) {
                view.showShop();
                return true ;
            }else{

                view.gotopartner();
                return false;
            }
        }


    }

    @Override
    public void init() {
        if (!TokenManager.INSTANCE.online()) {
            view.showAll();
            view.showHome();
            return;
        }
        startRequest(UserInfoModel
                .loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new BaseConsumer<UserInfo>() {
                            @Override
                            protected void error(String code, String msg) {
                                //view.shownetpopview();
                            }

                            @Override
                            protected void successAccept(UserInfo userInfo) {
                                UserInfoManger.INSTANCE.init(userInfo);
                                MobclickAgent.onProfileSignIn(UserInfoManger.INSTANCE.getUser().getUseraccount()+"");
                                if (UserInfoManger.INSTANCE.getUser().getActiviteStatus()==1){
                                    setdeviceinfo();
                                }else{
                                    view.showAll();
                                    view.showHome();
                                }

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {

                              // view.shownetpopview();

                           //    view.shownetpopview();

                            }
                        }));
    }

    @Override
    public void setClientid(String clientid) {
        if (TokenManager.INSTANCE.online()) {
            startRequest(LoginModel.updateAppAlias(clientid)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new BaseConsumer<Object>() {
                        @Override
                        protected void error(String code, String msg) {

                        }

                        @Override
                        protected void successAccept(Object o) {

                        }
                    }));
        }
    }

    @Override
    public void setNotice() {
        NoticeModel.getnotice()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<NoticeModel.NoticeBean>>() {
                    @Override
                    public void accept(BaseResult<NoticeModel.NoticeBean> noticeBeanBaseResult) throws Exception {
                        if (noticeBeanBaseResult.businessLogicSuccess()) {
                            if (noticeBeanBaseResult.getData() == null) {
                                return;
                            } else {

                                view.showNotice(noticeBeanBaseResult.getData());
                            }

                        } else {
                            view.showToast(noticeBeanBaseResult.getMessage() + "");
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage() + "");
                    }
                });


    }

    Boolean isshowOutline = true;
    public void setdeviceinfo() {
        DeviceModel.getdeviceinfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<DeviceModel.DeviceInfoBean>>() {
                    @Override
                    public void accept(BaseResult<DeviceModel.DeviceInfoBean> deviceInfoBaseResult) throws Exception {
                        if (deviceInfoBaseResult.businessLogicSuccess()) {
                            DeviceModel.DeviceInfoBean deviceInfoBean = deviceInfoBaseResult.getData();
                            DeviceInfo.Holder.getInstance().setAreaInfo(deviceInfoBean.getAreaInfo());
                            DeviceInfo.Holder.getInstance().setDevNo(deviceInfoBean.getDevNo());
                            DeviceInfo.Holder.getInstance().setAepnType(deviceInfoBean.getAepnType());
                            DeviceInfo.Holder.getInstance().setLocalVersion(deviceInfoBean.getLocalVersion());
                            DeviceInfo.Holder.getInstance().setBattery(deviceInfoBean.getBattery());
                            DeviceInfo.Holder.getInstance().setUpdateDate(deviceInfoBean.getUpdateDate());
                            DeviceInfo.Holder.getInstance().setOnlineState(deviceInfoBean.getOnlineState());
                            if (deviceInfoBean.getOnlineState().equals("2")){
                                if(isshowOutline){
                                    view.showoutline();
                                    isshowOutline=false;
                                }


                            }


                        } else {
                            view.showToast(deviceInfoBaseResult.getMessage() + "");
                        }
                        view.showAll();
                        view.showHome();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (throwable==null||throwable.equals("")){

                        }else{
                            view.showToast(throwable.getMessage() + "");
                        }

                    }
                });
    }

    public void getUserInfo() {

        UserInfoModel.loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<BaseResult<UserInfo>>() {
                            @Override
                            public void accept(BaseResult<UserInfo> baseResult) throws Exception {
                                if (baseResult.businessLogicSuccess()) {
                                    UserInfoManger.INSTANCE.clear();
                                    UserInfoManger.INSTANCE.init(baseResult.getData());
                                } else {
                                    view.showToast(baseResult.getMessage());
                                }
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                view.showToast(throwable.getMessage());
                            }
                        });
    }
}